﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using CollegeShop.Api.Report;
using CollegeShop.Api.User;
using CollegeShop.Api.ViewObject.User;
using CollegeShop.Api.ViewObject.Base;
using CollegeShop.Api.Repository;
using CollegeShop.Api.ViewObject.Commodity;
using CollegeShop.Api.ViewObject.Order;
using CollegeShop.Api.Commodity;
using CollegeShop.Api.Framework;

namespace CollegeShop.Api
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class CollegeShopAPI : System.Web.Services.WebService
    {
        #region user
        [WebMethod(Description = "通过用户名密码获取用户信息")]
        public string checkUser(int college, string userName, string password)
        {
            var data = AccountBiz.Instance.GetUserWithPassword(college, userName, password);
            if (data != null)
            {
                return data.ToJson();
            }
            return string.Empty;
        }
        [WebMethod(Description = "获取匿名用户的配送配送地址信息")]
        public string GetUserSetDelivery(int college, string userName)
        {
            var data = AccountBiz.Instance.GetDeliveryInfos(college, userName);
            if (data != null)
            {
                return data.ToJson();
            }
            return string.Empty;
        }
        [WebMethod(Description = "获取匿名用户的配送配送地址信息 clientType：安卓（1）、浏览器（4）")]
        public string GetAnonymousSetDelivery(int college, string anonymousCode, int ClientType)
        {
            var data = AccountBiz.Instance.GetDeliveryInfos(college, anonymousCode, ClientType);
            if (data != null)
            {
                return data.ToJson();
            }
            return string.Empty;
        }
        #endregion

        #region commodity
        [WebMethod(Description = "获取商品分类")]
        public string GetCommodityType(int college)
        {
            List<CommodityTypeVO> list = new List<CommodityTypeVO>();
            var dict = CommodityBiz.Instance.GetCommodityType(college);
            foreach (var item in dict)
            {
                list.Add(item.Value);
            }
            return list.ToJson();
        }


        [WebMethod(Description = "根据商品分类ID获取该分类下所有商品,page:0开始")]
        public string GetCommodities(int college, int category, int page, int pageSize)
        {
            var data = CommodityBiz.Instance.GetCommodities(college, category, page, pageSize);
            if (data != null)
            {
                return data.ToJson();
            }
            return string.Empty;
        }
        [WebMethod(Description = "获取商品")]
        public string GetCommodity(int college, int id)
        {
            var data = CommodityBiz.Instance.GetCommodity(college, id);
            if (data != null)
            {
                return data.ToJson();
            }
            return string.Empty;
        }
        [WebMethod(Description = "添加或修改商品")]
        public string SaveCommodity(int college, CommodityVO commodityVO)
        {
            return CommodityBiz.Instance.SaveCommodity(college, commodityVO) ? "true" : "false";
        }
        [WebMethod(Description = "删除商品")]
        public string DelCommodity(int college, string ids, string idResources)
        {
            return CommodityBiz.Instance.DelCommodity(college, ids, idResources) ? "true" : "false";
        }

        #endregion

        #region base
        [WebMethod(Description = "获取全部楼栋信息")]
        public string AllDormitoriy(int college)
        {
            var data = BaseBiz.Instance.GetAllDormitory(college);
            if (data != null)
            {
                return data.ToJson();
            }
            return string.Empty;
        }

        #endregion


        #region 图片处理
        /// <summary>
        /// 上传图片
        /// </summary>
        [WebMethod(Description = "上传图片", EnableSession = true)]
        public string UploadImage(byte[] fs, string FileName)
        {
            string url = string.Empty;
            try
            {
                url = ConfigHelper.GetConfigString("ImgUrl") + FileName;
                if (ImageHelper.UploadImage(fs, url))
                    return url;
            }
            catch (Exception ex)
            {

            }
            return string.Empty;
        }
        #endregion

        #region 安卓
        [WebMethod(Description = "获取商品分类")]
        public string GetCommodityTypeForAndroid(int college)
        {
            List<CommodityTypeVO> list = new List<CommodityTypeVO>();
            //var dict = CommodityBiz.Instance.GetCommodityType();
            //foreach (var item in dict)
            //{
            //    list.Add(item.Value);
            //}
            CommodityTypeVO item = new CommodityTypeVO();
            item.Id = 1;
            item.Name = "休闲食品";
            list.Add(item);

            item = new CommodityTypeVO();
            item.Id = 2;
            item.Name = "充饥食品";
            list.Add(item);


            item = new CommodityTypeVO();
            item.Id = 3;
            item.Name = "营养饮品";
            list.Add(item);

            item = new CommodityTypeVO();
            item.Id = 4;
            item.Name = "时尚小吃";
            list.Add(item);

            item = new CommodityTypeVO();
            item.Id = 0;
            item.Name = "全部";
            list.Add(item);


            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("commoditytype", list);
            dic.Add("response", "success");

            string resultStr = dic.ToJson();
            return resultStr;

        }

        [WebMethod(Description = "获取全部楼栋信息")]
        public string GetDormitoriyForAndroid(int college)
        {
            List<DormitoryVO> domitorylist = new List<DormitoryVO>();
            DormitoryVO item = new DormitoryVO();
            item.Id = 1;
            item.DormitoryName = "梅园1号楼";
            domitorylist.Add(item);

            item = new DormitoryVO();
            item.Id = 2;
            item.DormitoryName = "梅园2号楼";
            domitorylist.Add(item);

            item = new DormitoryVO();
            item.Id = 3;
            item.DormitoryName = "梅园3号楼";
            domitorylist.Add(item);

            item = new DormitoryVO();
            item.Id = 4;
            item.DormitoryName = "梅园4号楼";
            domitorylist.Add(item);

            item = new DormitoryVO();
            item.Id = 5;
            item.DormitoryName = "梅园5号楼";
            domitorylist.Add(item);

            item = new DormitoryVO();
            item.Id = 6;
            item.DormitoryName = "竹园1号楼";
            domitorylist.Add(item);

            item = new DormitoryVO();
            item.Id = 7;
            item.DormitoryName = "竹园2号楼";
            domitorylist.Add(item);

            item = new DormitoryVO();
            item.Id = 8;
            item.DormitoryName = "竹园3号楼";
            domitorylist.Add(item);

            item = new DormitoryVO();
            item.Id = 9;
            item.DormitoryName = "竹园4号楼";
            domitorylist.Add(item);

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("domitorylist", domitorylist);
            dic.Add("response", "success");


            string resultStr = dic.ToJson();
            return resultStr;
        }

        [WebMethod(Description = "获取全部学校信息")]
        public string GetCollegeForAndorid()
        {
            List<CollegeVO> list = new List<CollegeVO>();
            CollegeVO item = new CollegeVO();
            item.Id = 1;
            item.Name = "重庆大学";
            list.Add(item);

            item = new CollegeVO();
            item.Id = 2;
            item.Name = "重庆师范大学";
            list.Add(item);

            item = new CollegeVO();
            item.Id = 2;
            item.Name = "重庆科技大学";
            list.Add(item);

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("collegelist", list);
            dic.Add("response", "success");


            string resultStr = dic.ToJson();
            return resultStr;
        }

        [WebMethod(Description = "分类获取商品信息")]
        public string GetProductByCategoryForAndroid(int college, int categoryId)
        {
            List<Commodity4AndroidVO> list = new List<Commodity4AndroidVO>();
            Commodity4AndroidVO item = new Commodity4AndroidVO();
            item.Id = 1;
            item.Name = "泡椒凤爪";
            item.Pic = "http://172.26.5.88/image/product/leshishupian.jpg";
            item.Price = 12.5m;
            list.Add(item);

            item = new Commodity4AndroidVO();
            item.Id = 2;
            item.Name = "泡椒牛肉干";
            item.Pic = "http://172.26.5.88/image/product/leshishupian(tong).jpg";
            item.Price = 15m;
            list.Add(item);

            item = new Commodity4AndroidVO();
            item.Id = 3;
            item.Name = "康师傅老坛酸菜牛肉面";
            item.Pic = "http://172.26.5.88/image/product/leshishupian.jpg";
            item.Price = 4.5m;
            list.Add(item);

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("productlist", list);
            dic.Add("response", "success");

            string resultStr = dic.ToJson();
            return resultStr;
        }
        [WebMethod(Description = "获取商品明细信息")]
        public string GetProductDetailForAndroid(int college, int goodsid)
        {
            List<CommodityDetail4AndroidVO> list = new List<CommodityDetail4AndroidVO>();
            CommodityDetail4AndroidVO item = new CommodityDetail4AndroidVO();
            item.Id = 1;
            item.Title = "康师傅泡椒牛肉面";
            item.Pic = "http://172.26.5.88/image/product/leshishupian.jpg";
            item.Price = 4m;
            item.Standard = "450g";
            item.Style = "泡椒牛肉";
            item.Description = "<p>好吃得很哦，吃的就是这个味道</p>";
            list.Add(item);

            item = new CommodityDetail4AndroidVO();
            item.Id = 2;
            item.Title = "康师傅红烧牛肉面";
            item.Pic = "http://172.26.5.88/image/product/leshishupian.jpg";
            item.Price = 4.5m;
            item.Standard = "450g";
            item.Style = "红烧牛肉面";
            item.Description = "<p>好吃得很哦，吃的就是这个味道,红烧牛肉面</p>";
            list.Add(item);

            item = new CommodityDetail4AndroidVO();
            item.Id = 3;
            item.Title = "康师傅老坛酸菜牛肉面";
            item.Pic = "http://172.26.5.88/image/product/leshishupian(tong).jpg";
            item.Price = 5m;
            item.Standard = "450g";
            item.Style = "老坛酸菜牛肉";
            item.Description = "<p>好吃得很哦，吃的就是这个味道,红烧牛肉面<img src=\"http://172.26.5.88/image/product/leshishupian(tong).jpg\"/></p>";
            list.Add(item);

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("productdetaillist", list);
            dic.Add("response", "success");

            string resultStr = dic.ToJson();
            return resultStr;
        }

        #region user
        [WebMethod(Description = "通过用户名密码获取用户信息")]
        public string CheckUserForAndroid(int college, string userName, string password)
        {
            var data = AccountBiz.Instance.GetUserWithPassword(college, userName, password);
            Dictionary<string, object> dic = new Dictionary<string, object>();
            if (data != null)
            {
                dic.Add("user", data);
                dic.Add("response", "success");
            }
            else
            {
                dic.Add("user", "");
                dic.Add("response", "error");
            }
            string resultStr = dic.ToJson();
            return resultStr;
        }
        [WebMethod(Description = "获取用户的配送配送地址信息")]
        public string GetUserSetDeliveryForAndroid(int college, string userName)
        {
            var data = AccountBiz.Instance.GetDeliveryInfos(college, userName);
            if (data != null)
            {
                return data.ToJson();
            }
            return string.Empty;
        }
        [WebMethod(Description = "获取匿名用户的配送配送地址信息 clientType：安卓（1）、浏览器（4）")]
        public string GetAnonymousSetDeliveryForAndroid(int college, string anonymousCode, int ClientType)
        {
            var data = AccountBiz.Instance.GetDeliveryInfos(college, anonymousCode, ClientType);
            if (data != null)
            {
                return data.ToJson();
            }
            return string.Empty;
        }
        #endregion
        [WebMethod(Description = "根据不同状态获取订单列表")]
        public string GetOrderListForAndorid(int college, int state, int pageNo, string username, int isLogin)
        {
            List<Order4AndroidVO> list = new List<Order4AndroidVO>();
            Order4AndroidVO item = new Order4AndroidVO();
            int count = 0;
            switch (state)
            {
                case 0:
                    for (int i = 1; i < 4; i++)
                    {
                        item = new Order4AndroidVO();
                        item.ID = i;
                        item.OrderNumber = "201312192030000" + i;
                        item.OrderTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        item.TotalMoney = 20;
                        item.State = "待发货";
                        list.Add(item);
                    }
                    count = 3;
                    break;
                case 1:
                    for (int i = 1; i < 4; i++)
                    {
                        if (i % 2 == 0)
                        {
                            item = new Order4AndroidVO();
                            item.ID = i;
                            item.OrderNumber = "201312192030222" + i;
                            item.OrderTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            item.TotalMoney = 20;
                            item.State = "待发货";
                            list.Add(item);
                        }
                        else
                        {
                            item = new Order4AndroidVO();
                            item.ID = i;
                            item.OrderNumber = "201312192030222" + i;
                            item.OrderTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            item.TotalMoney = 20;
                            item.State = "交易完成";
                            list.Add(item);
                        }
                    }
                    count = 4;
                    break;
                case 2:
                    for (int i = 1; i < 11; i++)
                    {
                        
                        if (i % 2 == 0)
                        {
                            item = new Order4AndroidVO();
                            item.ID = i;
                            item.OrderNumber = "201312192030222" + i+"页"+pageNo;
                            item.OrderTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            item.TotalMoney = 20;
                            item.State = "待发货";
                            list.Add(item);
                        }
                        else
                        {
                            item = new Order4AndroidVO();
                            item.ID = i;
                            item.OrderNumber = "201312192030222" + i + "页" + pageNo;
                            item.OrderTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            item.TotalMoney = 20;
                            item.State = "交易完成";
                            list.Add(item);
                        }
                    }
                    count = 40;
                    break;
            }



            
            Dictionary<string, object> dic = new Dictionary<string, object>();

            dic.Add("orderlist", list);
            dic.Add("count", count.ToString());
            dic.Add("response", "success");

            string resultStr = dic.ToJson();
            return resultStr;
        }

        #endregion

    }
}
